package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

/**
 * SubChoiceFrameResp类
 * 实现帧响应选择的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class SubChoiceFrameResp implements AxdrType {

    private byte[] dataCode = null;
    private SubChoiceFrameRespChoice choice = SubChoiceFrameRespChoice._ERR_NONE_SELECTED;
    private AxdrEnum dar = null;
    private SubSeqOfResults attr = null;
    private SubSeqOfRecordResults recordAttr = null;

    /**
     * 默认构造函数
     */
    public SubChoiceFrameResp() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置DAR
     * 
     * @param dar 数据访问结果
     */
    public void setDar(AxdrEnum dar) {
        this.resetChoices();
        this.choice = SubChoiceFrameRespChoice.DAR;
        this.dar = dar;
    }

    /**
     * 设置属性序列
     * 
     * @param attr 属性序列
     */
    public void setAttr(SubSeqOfResults attr) {
        this.resetChoices();
        this.choice = SubChoiceFrameRespChoice.ATTR;
        this.attr = attr;
    }

    /**
     * 设置记录属性序列
     * 
     * @param recordAttr 记录属性序列
     */
    public void setRecordAttr(SubSeqOfRecordResults recordAttr) {
        this.resetChoices();
        this.choice = SubChoiceFrameRespChoice.RECORD_ATTR;
        this.recordAttr = recordAttr;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = SubChoiceFrameRespChoice.fromValue((int) choosen.getValue());

        if (this.choice == SubChoiceFrameRespChoice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.setConst();
            codeLength += this.dar.decode(input);
            return codeLength;
        } else if (this.choice == SubChoiceFrameRespChoice.ATTR) {
            this.attr = new SubSeqOfResults();
            codeLength += this.attr.decode(input);
            return codeLength;
        } else if (this.choice == SubChoiceFrameRespChoice.RECORD_ATTR) {
            this.recordAttr = new SubSeqOfRecordResults();
            codeLength += this.recordAttr.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == SubChoiceFrameRespChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == SubChoiceFrameRespChoice.RECORD_ATTR) {
            if (this.recordAttr != null) {
                codeLength += this.recordAttr.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("recordAttr is null");
            }
        } else if (this.choice == SubChoiceFrameRespChoice.ATTR) {
            if (this.attr != null) {
                codeLength += this.attr.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("attr is null");
            }
        } else if (this.choice == SubChoiceFrameRespChoice.DAR) {
            codeLength += this.dar.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding SubChoiceFrameResp", e);
        }
        this.dataCode = revOStream.getArray();
    }

    /**
     * 获取选择索引
     * 
     * @return 选择索引
     */
    public SubChoiceFrameRespChoice getChoiceIndex() {
        return this.choice;
    }

    /**
     * 获取DAR
     * 
     * @return DAR
     */
    public AxdrEnum getDar() {
        return this.dar;
    }

    /**
     * 获取属性序列
     * 
     * @return 属性序列
     */
    public SubSeqOfResults getAttr() {
        return this.attr;
    }

    /**
     * 获取记录属性序列
     * 
     * @return 记录属性序列
     */
    public SubSeqOfRecordResults getRecordAttr() {
        return this.recordAttr;
    }

    /**
     * 重置选择
     */
    public void resetChoices() {
        this.choice = SubChoiceFrameRespChoice._ERR_NONE_SELECTED;
        this.dar = null;
        this.attr = null;
        this.recordAttr = null;
    }

    @Override
    public String toString() {
        if (this.choice == SubChoiceFrameRespChoice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else if (this.choice == SubChoiceFrameRespChoice.ATTR) {
            if (this.attr != null) {
                return "choice: {attr: " + this.attr + "}";
            } else {
                return "choice is attr but attr is null";
            }
        } else if (this.choice == SubChoiceFrameRespChoice.RECORD_ATTR) {
            if (this.recordAttr != null) {
                return "choice: {recordAttr: " + this.recordAttr + "}";
            } else {
                return "choice is recordAttr but recordAttr is null";
            }
        } else {
            return "unknown";
        }
    }
}